package cn.edu.lingnan.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.IntBuffer;

public class AuthorityFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest) request;
        HttpServletResponse resp=(HttpServletResponse) response;
        HttpSession session=req.getSession();
        String str=(String)session.getAttribute("sright");
        System.out.println("[DEBUG 020]该用户的权限为"+str);
        if(str!=null){//这里是登录的情况
            int sright=Integer.parseInt(str);
            if(sright==1){//管理员
                chain.doFilter(request, response);
            }else{//普通用户（学生）
                resp.sendRedirect(req.getContextPath()+"/authority.html");
            }
        }else{//没登录
            resp.sendRedirect(req.getContextPath()+"/login.html");
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}
